<?php

namespace App\Http\Middleware\Auth;

use App\Models\ApiResult;
use App\Models\User;
use App\Services\Base\CacheService;
use App\Services\Base\MsgService;
use Closure;
use Illuminate\Http\JsonResponse;

class CheckPayPwd
{
    /**
     * 支付密码未设置
     * @param $request
     * @param Closure $next
     * @return JsonResponse|mixed
     */
    public function handle($request, Closure $next)
    {

        $token = $request->bearerToken();
        $user = CacheService::hGet(R_LOGIN_TOKEN_INFO, $token);
        if (!$user) {
            return response()->json(ApiResult::failure(401, MsgService::toMessage(401)));
        }

        /**
         * @var $user User
         */
        $user = User::query()->find($user['uid']);
        if (!$user->pay_password) { //请设置支付密码
            return response()->json(ApiResult::failure(1011, MsgService::toMessage(1011)));
        }

        return $next($request);
    }
}
